package net.jforum.util;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileReader;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.Enumeration;
import java.util.Properties;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.apache.log4j.Logger;
import org.hsqldb.lib.StringUtil;

import net.jforum.JForumBaseServlet;
import net.jforum.util.preferences.ConfigKeys;
import net.jforum.util.preferences.SystemGlobals;

/**
 * @ author  张洋 jackson.zhang@ericsson.com
 * @ version 创建时间 ：May 23, 2010  4:47:33 PM
 * @ description : 
 */
public class KeywordFilter {
	private static Pattern keywordPattern=null;  
	private static Logger logger = Logger.getLogger(KeywordFilter.class);
	   public  static void initial(String configPath){
  		if (!new File(configPath).exists()) {
				return;
			}
 	  		StringBuilder sb=new StringBuilder();
	  		try {
	  			FileInputStream input = new FileInputStream(configPath);
	  			
	            Properties pro = new Properties();  
	            pro.load(input);
	  			Enumeration<?> enu = pro.propertyNames();
	  		    
	  			while (enu.hasMoreElements()) {
	  				String key=((String)enu.nextElement()).trim();
	  				if("".equals(key.trim()))continue;
	  				if(!key.endsWith("|")&&enu.hasMoreElements())
	  				key=key+"|";  				
	  				sb.append(key);
	  				}	
	  			String c=new String(sb.toString().trim().getBytes(
				"ISO-8859-1"), "UTF-8");
	  			keywordPattern=Pattern.compile(c
	  					, Pattern.CASE_INSENSITIVE);
	  		} catch (Exception ex) {
	  			ex.printStackTrace();
	  			logger.error("key word filter pattern generate error!");
	  		}		
	  	}
		public static String filter(String content){

 			if(!SystemGlobals.getBoolValue(ConfigKeys.KEYWORD_FILTER_ENABLE))
 				 return content;
			if(null==content||content.length()<1)return"";
			Matcher m=keywordPattern.matcher(content);
			if(m.find())
            content= m.replaceAll(SystemGlobals.getValue(ConfigKeys.BAD_WORDS_DEFAULT_REPLACEMENT));
				return content;
		}
 
		public static String getFileContent(String filePath) throws IOException{
			FileReader fr=new FileReader(new File(filePath));
			BufferedReader br=new BufferedReader(fr);
			String content=null;
		     StringBuilder sb=new StringBuilder();         
			 while((content=br.readLine())!=null){
				 if(content.trim().length()>1)
				 sb.append(content+"\n");
			 }
			return sb.toString();
		}
		public static void main(String args[]) throws IOException{
			KeywordFilter.initial("C:/JacksonZhang/ProjectWorkBeach/BranchView/Test/KeywordTest/src/keywords.properties");
			 String afterFilter=null;
			 System.out.println(System.currentTimeMillis());
			try{
              afterFilter=KeywordFilter.filter(getFileContent("C:/JacksonZhang/ProjectWorkBeach/BranchView/Test/KeywordTest/src/testFile.txt"));
			}catch(Exception e){
				e.printStackTrace();
			}
			System.out.println(System.currentTimeMillis());
			System.out.println(afterFilter);
		}
	
}
